package controller.frontOffice;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.event.DragDropEvent;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;

import org.primefaces.model.UploadedFile;

import controller.backOffice.ClientController;

import annotation.LoggedIn;

import ejb.author.AuthorService;
import ejb.book.BookService;
import ejb.order.OrderService;
import entities.Author;
import entities.Book;
import entities.Category;
import entities.Client;
import entities.Order;
import entities.OrderItem;

@Named
@SessionScoped
public class CartController implements Serializable {

	private static final long serialVersionUID = 1L;

	private transient @Inject Order order; 
	
	private transient @Inject OrderItem orderItem;	
	
	private transient @Inject Book selectedBook;
	
   // private transient @ManagedProperty(value = "#{clientController}") ClientController clientController;
	
	private BigDecimal totalCount;
	
	@EJB
	private BookService bookService;
	
	@EJB
	private OrderService orderService;
	
	//private List<Client> clients;
	public String addToCart() {
		
		order.addOne(getBookByParamId("bookId"));
		return "";
	}
	
	public String removeOneOfCart() {
		order.removeOne(getBookByParamId("bookId"));
		return "";
	}

	public Book getBookByParamId(String param) {
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String bookId1 = (String)requestMap.get(param);  
		Long bookId = Long.parseLong(bookId1);
		return (Book) bookService.find(bookId);
	}

	public Order getOrder() {
		setTotalCount(order.getTotal());
		return order;
	}
	
	public void setOrder(Order order) {
		this.order = order;
	}
	
	public OrderItem getOrderItem() {
		return orderItem;
	}
	
	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}
	
	public Book getSelectedBook() {
		return selectedBook;
	}
	
	public void setSelectedBook(Book selectedBook) {
		this.selectedBook = selectedBook;
	}

	public BigDecimal getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(BigDecimal totalCount) {
		this.totalCount = totalCount;
	}
	
	public void onDrop(DragDropEvent event) {  
	   System.out.println("onDrop");
       Book book = (Book) event.getData(); 
       System.out.println("Book inserted : "+ book.getTitle());              
       order.addOne(book);  
  
        //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(book.getTitle() + " added", "Position:" + event.getDropId()));  
    } 
}
